<script setup name="deviceList">
import { GetPlannedProjectAPI } from "@/api/device/maintenance/project.js";

const { proxy } = getCurrentInstance();

// const { sys_material_type } = proxy.useDict('sys_material_type')
const props = defineProps({
  isShow: {
    type: Boolean,
    default: false,
  },

  title: {
    type: String,
    default: "物料",
  },
  // 是否单选
  bdb: {
    type: Boolean,
    default: false,
  },
  ids: {
    type: Array,
    default: () => [],
  },
});

const emit = defineEmits(["selectsubmit", "preservation", "cancel", "reset"]);

const loading = ref(false);

const typeSelect = ref([]);

// 查询条件
const query = reactive({
  pageNum: 1,
  pageSize: 10,
  materialName: "", // 名称
  materialCode: "", // 编码
  materialType: "", // 物料类别
  type: "", // 类别
});

const handleSelect = ref({ id: "" });
const handleSelects = ref([]);
const TableData = ref([]);
const total = ref(0);

// 勾选数据
const handleSelectionChange = (row) => {
  handleSelect.value = row;
};

// 勾选数据
const handleSelectionChanges = (row) => {
  handleSelects.value = row;
};

// 是否禁用 保存
const d = computed(() => {
  if (props.bdb) {
    return handleSelect.value?.id ? false : true;
  } else {
    return handleSelects.value.length > 0 ? false : true;
  }
});

// 提交
const preservation = () => {
  if (props.bdb) {
    emit("preservation", handleSelect.value);
  } else {
    emit("preservation", handleSelects.value);
  }
};

// 重置
const reset = () => {
  for (let key in query) {
    query[key] = "";
  }

  getList();
};

// 查询
const getList = async () => {
  loading.value = true;
  try {
    query.projectIds = props.ids.join(",") || "";
    const res = await GetPlannedProjectAPI(query);
    total.value = parseFloat(res.total);
    TableData.value = res.rows;
  } catch {
  } finally {
  }
  loading.value = false;
};
// getList()
/**
 * 清空
 */
const close = () => {
  Object.keys(query).map((t) => (query[t] = ""));
  emit("cancel");
};
watch(
  () => props.isShow,
  (v) => {
    if (props.isShow) getList();
  }
);
</script>
<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <p-dialog
      :isBtn="false"
      v-model="props.isShow"
      title="项目信息"
      @close="close"
      width="50%"
    >
      <template #body>
        <el-form :inline="true" label-width="100px">
          <el-form-item label="项目编码">
            <el-input
              clearable
              v-model.trim="query.maintenanceProjectCode"
              placeholder="请输入项目编码"
            />
          </el-form-item>
          <el-form-item label="项目名称">
            <el-input
              clearable
              v-model.trim="query.maintenanceProjectName"
              placeholder="请输入项目名称"
            />
          </el-form-item>
          <el-form-item label="维保部位">
            <el-input
              clearable
              v-model.trim="query.maintenancePosition"
              placeholder="请输入维保部位"
            />
          </el-form-item>
        </el-form>

        <el-divider />
        <el-table
          stripe
          v-if="!loading"
          border
          :loading="loading"
          :data="TableData"
          highlight-current-row
          @current-change="handleSelectionChange"
          @selection-change="handleSelectionChanges"
        >
          <el-table-column label="序号" width="80" type="index" />
          <el-table-column width="80" v-if="props.bdb">
            <template #default="scope">
              <el-radio-group v-model="handleSelect.id">
                <el-radio :label="scope.row.id" size="large">&nbsp;</el-radio>
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column
            v-else
            type="selection"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="项目编号"
            prop="maintenanceProjectCode"
            width="180"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="项目名称"
            prop="maintenanceProjectName"
            width="180"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="项目类型"
            prop="projectType"
            width="180"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="设备编码"
            prop="deviceCode"
            width="180"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="设备名称"
            prop="deviceName"
            width="180"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="保养部位"
            prop="maintenancePosition"
            width="auto"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="保养级别"
            prop="maintenanceLevelDict"
            width="auto"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="标准工时"
            prop="workingHour"
            width="120"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="保养内容"
            prop="maintenanceContent"
            width="auto"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="备注"
            prop="remark"
            width="auto"
            align="center"
            show-overflow-tooltip
          />
        </el-table>
        <div class="pagination">
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="query.pageNum"
            v-model:limit="query.pageSize"
            @pagination="getList"
          />
        </div>
        <!-- 按钮 -->
        <div>
          <el-row class="Btn">
            <el-button type="primary" @click="getList">查询列表</el-button>
            <el-button type="info" @click="reset">重置</el-button>
            <el-button type="success" @click="preservation" :disabled="d"
              >保存</el-button
            >
            <el-button type="info" @click="emit('cancel')">取消</el-button>
          </el-row>
        </div>
      </template>
    </p-dialog>
  </div>
</template>

<style scoped lang="scss">
.Btn {
  margin-top: 10px;
  float: right;
}
</style>
